from __future__ import absolute_import, unicode_literals
import requests
import hashlib
from core.tools.custom_requests import CustomRequests
from hashlib import sha256
import hmac
import base64


class BaseYLClient(CustomRequests):
    _http = requests.Session()

    def sha256_hex(slef, content):
        x = hashlib.sha256()
        x.update(content.encode())
        return x.hexdigest()

    def HmacSHA256(self, data, app_key):
        signature = hmac.new(app_key.encode('utf-8'), data.encode('utf-8'), digestmod=hashlib.sha256).digest()
        signature = base64.b64encode(signature)
        return signature

    def custome_json(self, data):
        r = '{'
        for k, v in data.items():
            if isinstance(v, int):
                r += '"' + k + '"' + ":" + str(v) + ","
            else:
                r += '"' + k + '"' + ":" + '"' + str(v) + '"' + ","
        r = r[:-1] + "}"
        return r
